package cn.llk.mall.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * http的结果
 *
 * @author pdd20
 * @date 2021/04/23
 */
@ApiModel
@Data
public class HttpResult<T> {
    public static final int OK = 0;
    private static final Integer ERROR = 101;
    @ApiModelProperty(value = "消息码", required = true, example = "0")
    private Integer code = 0;
    @ApiModelProperty(value = "提示信息。主要用于开发调试，不建议显示给用户", required = true)
    private String message;
    @ApiModelProperty(value = "返回包体", required = true)
    private T data;
    @ApiModelProperty(value = "是否正常返回", required = true, example = "true", notes = "主要预留给某些不方便判断code的场景，只要code不是0，这个字段就是false")
    private boolean success = true;

    public HttpResult() {}

    public HttpResult(T data) {
        this.data = data;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
        if (code != OK) {
            this.success = false;
        }
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return this.success;
    }

    @Override
    public String toString() {
        return "HttpResult [code=" + code + ", message=" + message + ", data=" + data + ", success=" + success + "]";
    }

    public static <T> HttpResult<T> ok(T data, String message) {
        HttpResult<T> res = new HttpResult<>();
        res.setData(data);
        res.setMessage(message);
        res.setCode(OK);
        return res;
    }

    public static <T> HttpResult<T> ok(T data) {
        HttpResult<T> res = new HttpResult<>();
        res.setData(data);
        res.setCode(OK);
        return res;
    }

    public static <T> HttpResult<T> ok(String message) {
        HttpResult<T> res = new HttpResult<>();
        res.setMessage(message);
        res.setCode(OK);
        return res;
    }

    public static <T> HttpResult<T> error(T data, String message,Integer code) {
        HttpResult<T> res = new HttpResult<>();
        res.setData(data);
        res.setMessage(message);
        res.setCode(code);
        return res;
    }

    public static <T> HttpResult<T> error(T data, String message) {
        HttpResult<T> res = new HttpResult<>();
        res.setData(data);
        res.setMessage(message);
        res.setCode(ERROR);
        return res;
    }

    public static <T> HttpResult<T> error(T data) {
        HttpResult<T> res = new HttpResult<>();
        res.setData(data);
        res.setCode(ERROR);
        return res;
    }

    public static <T> HttpResult<T> error(String message) {
        HttpResult<T> res = new HttpResult<>();
        res.setMessage(message);
        res.setCode(ERROR);
        return res;
    }

}